<include file="Common:header"/>
<include file="Common:left"/>
<div id="my-main" class="floatleft">
    <div class="user_enjoy">
        <div class="info_title clearfix">
            <h2 class="setting_tilte floatleft">我的设置</h2>
        </div>
        <ul class="status_tab">
            <li><a href="__URL__/index">详细资料</a></li>
            <li class="act"><a href="__URL__/passwd">修改密码</a></li>
            <li><a href="__URL__/photo">更换头像</a></li>
        </ul>
        <!-- 第三方登录的用户需要先绑定账号 -->
        <if condition="$display_type eq 3">
            <h1>请先绑定账号</h1>

            <div class="cRow" style="margin-top:10px"><i>用户邮箱：</i><input type="text" name="bind_user_email"
                                                                         id="bind_user_email"
                                                                         class="input150"/><span></span></div>
            <div class="cRow" style="margin-top:10px"><i>用户密码：</i><input type="password" name="bind_user_passwd"
                                                                         id="bind_user_passwd" maxlength="20"
                                                                         class="input150"/><span></span></div>
            <div class="cRow" style="margin-top:10px"><i>输入结果：</i><input type="text" name="auth" id="auth"
                                                                         class="input150" style="width:50px"
                                                                         maxlength="2"/>&nbsp;<img
                    src="__APP__/Public/authcode" id="auth_image" alt="点击刷新验证码" title="点击刷新验证码"

                    onclick="fleshVerify();"/><span></span></div>
            <div class="editBtn"><a class="w60r" href="javascript:void(0);" onclick="checkAccountData();">绑&nbsp;定</a>
            </div>
        </if>
        <!-- 去绑定邮箱验证 -->
        <if condition="$display_type eq 2">
            <div class="cRow" style="margin-top:10px">{$UserInfo['user_nickname']}，您好！请前往{$bind_email}验证！
            </div>
        </if>
        <!-- 修改密码 -->
        <if condition="$display_type eq 1">
            <div class="cRow" style="margin-top:10px"><i>原始密码：</i><input type="password" name="old_password"
                                                                         id="old_password" maxlength="20"
                                                                         class="input150"/><span></span>
            </div>
            <div class="cRow"><i>新的密码：</i><input type="password" name="new_password" id="new_password" maxlength="20"
                                                 class="input150"/><span></span></div>
            <div class="cRow"><i>再次确认：</i><input type="password" name="new_password_confirm" id="new_password_confirm"
                                                 maxlength="20" class="input150"/><span></span></div>
            <div class="editBtn"><a class="w60r" href="javascript:void(0);" onclick="checkData();">确&nbsp;认</a></div>
        </if>
    </div>
</div>
</div>
<script type="text/javascript">
    //通过js来获取包含中文字符的字符串的长度
    function getLen(str) {
        var totallength = 0;
        for (var i = 0; i < str.length; i++) {
            //charCodeAt 返回一个整数，获得字符的unicode编码
            var intCode = str.charCodeAt(i);
            if (intCode >= 0 && intCode <= 128) {
                //为字符的编码范围
                totallength = totallength + 1;//非中文单个字符长度加1
            } else {
                totallength = totallength + 2;//中文字符长度则加2
            }
        }
        return totallength;
    }

    //正则匹配邮箱
    function isEmail(str) {
        var patrn = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
        if (!patrn.exec(str) || getLen(str) > 40) return false
        return true
    }

    //正则匹配密码
    function isPasswd(str) {
        var patrn = /^(\w){6,20}$/;
        if (!patrn.exec(str)) return false
        return true
    }

    //重载验证码
    function fleshVerify() {
        var timenow = new Date().getTime();
        $('#auth_image').attr('src', _APP_ + '/Public/authcode/' + timenow);
    }

    //校验绑定账号数据
    function checkAccountData() {
        var bind_user_email = $('#bind_user_email').val();
        var bind_user_passwd = $('#bind_user_passwd').val();
        var bind_auth = $('#auth').val();//验证码
        $('#bind_user_email').next().html('');
        $('#bind_user_passwd').next().html('');
        if (!isEmail(bind_user_email)) {
            $('#bind_user_email').next().html('邮箱格式错误或长度超过40位');
        } else {
            if (!isPasswd(bind_user_passwd)) {
                $('#bind_user_passwd').next().html('密码为空或格式错误');
            } else {
                //邮箱和密码格式正确，那么绑定账号
                showBoxBg();//显示遮罩层
                $.ajax({
                    dataType: "text",
                    type: "POST",
                    url: _URL_ + "/bindAccount",
                    data: "user_email=" + bind_user_email + "&user_passwd=" + bind_user_passwd + "&auth=" + bind_auth,
                    success: function (val) {
                        if (val == 1) {
                            //验证码错误
                            closeBoxBg();//关闭遮罩层
                            $('#auth_image').next().html('请输入正确的答案！');
                        } else if (val == 2) {
                            //邮箱已占用
                            closeBoxBg();//关闭遮罩层
                            $('#bind_user_email').next().html('此邮箱已被其他人使用！');
                        } else if (val == 3) {
                            //下发邮件了，也更新数据了，提醒用户去邮箱验证
                            $('#common-notice-text').html('绑定还差最后一步，请去邮箱验证！');
                            showBox('#common-notice');
                            jumpurlTime(_URL_ + '/passwd', 2000);
                        } else if (val == 4) {
                            $('#common-notice-text').html('绑定失败！请稍后再试！');
                            showBox('#common-notice');
                            jumpurlTime(_URL_ + '/passwd', 2000);
                        }
                    },
                    error: function () {
                        $('#bind_user_email').next().html('服务器没有返回数据请稍后再试！');
                    }
                });
            }
        }
    }


    //正则匹配密码
    function isPasswd(str) {
        var patrn = /^(\w){6,20}$/;
        if (!patrn.exec(str)) return false
        return true
    }

    //校验修改密码数据
    function checkData() {
        $('#old_password,#new_password,#new_password_confirm').next().html('');
        var old_password = $('#old_password').val();
        if (!isPasswd(old_password)) {
            $('#old_password').next().html('原始密码格式错误！');
            return false;
        }
        var new_password = $('#new_password').val();
        if (!isPasswd(new_password)) {
            $('#new_password').next().html('密码长度为6-20位，由数字、英文字母组成！');
            return false;
        }
        var new_password_confirm = $('#new_password_confirm').val();
        if (new_password != new_password_confirm) {
            $('#new_password_confirm').next().html('两次密码不一致！');
            return false;
        }
        showBoxBg();//显示遮罩层
        $.ajax({
            dataType: "json",
            type: "POST",
            url: _URL_ + "/checkPasswd",
            data: "old_password=" + old_password + "&new_password=" + new_password,
            success: function (json) {
                if (json.status == 1) {
                    closeBoxBg();//关闭遮罩层
                    $('#old_password').next().html(json.text);
                    $('#new_password').next().html('');
                    $('#new_password_confirm').next().html('');
                } else {
                    $('#common-notice-text').html(json.text);
                    showBox('#common-notice');
                    jumpurlTime(_APP_ + '/Public/login', 2000);
                }
            }
        })
    }
</script>
<!-- 普通提示层 -->
<div id="common-notice" class="alert-wrap">
    <div class="alert-wrap-header"><i class="bg"></i>温馨提示&nbsp;</div>
    <div id="common-notice-text"></div>
    <a class="close-alert-wrap bg" href="javascript:void(0)" onclick="closeDialog('#common-notice')" title="关闭"></a>
</div>
<include file="Common:footer"/>
